
package view
{
    import constants.LoadingFlags;

import model.ElfinProxy;

import model.LogProxy;
    import model.UserProxy;
    import model.vo.UserVO;

    import mx.core.FlexGlobals;
    import mx.core.UIComponent;

    import org.kolonitsky.alexey.debug.DebugWrapper;
    import org.kolonitsky.alexey.mvcext.NotificationMap;
    import org.kolonitsky.alexey.mvcext.events.PageDescriptor;
    import org.kolonitsky.alexey.mvcext.events.PageEvent;
    import org.puremvc.as3.interfaces.IMediator;

    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.mediator.Mediator;

    public class AppMediator extends Mediator
    {
        public static const NAME:String = "ElfinMediator";



        public function AppMediator()
        {
            DebugWrapper.trace("INFO: AppMediator created.");
            super(NAME, app)
        }

        public function get app():Elfin
        {
            var result:Elfin = FlexGlobals.topLevelApplication as Elfin;
            return result;
        }




        //-------------------------------------------------------------------------
        //
        // Mediator overriden methods
        //
        //-------------------------------------------------------------------------

        override public function onRegister():void
        {
            app.addEventListener(PageEvent.SWITCH_PAGE, app_switchPageHandler);

            sendNotification(NotificationMap.UPDATE_PAGE);
        }

        override public function onRemove():void
        {
            app.removeEventListener(PageEvent.SWITCH_PAGE, app_switchPageHandler);
        }

        override public function listNotificationInterests():Array
        {
            return [
                NotificationMap.SWITCH_PAGE,
                LogProxy.UPDATE,
                UserProxy.USER_AUTHORIZATION
            ]
        }

        override public function handleNotification(notification:INotification):void
        {
            switch (notification.getName())
            {
                case UserProxy.USER_AUTHORIZATION:
                {
                    var user:UserVO = notification.getBody() as UserVO;
                    if (user)
                    {
                        app.user = user;
                        sendNotification(NotificationMap.UPDATE_PAGE);
                    }
                    break;
                }

                case NotificationMap.SWITCH_PAGE:
                {
                    if (app.user && app.user.authorized)
                    {
                        switchPage(notification.getBody() as PageDescriptor);
                    }
                    break;
                }
            }
        }




        //-------------------------------------------------------------------------
        //
        // Private logic
        //
        //-------------------------------------------------------------------------

        public var mediators:Array = [];

        private function switchPage(page:PageDescriptor):void
        {
            if ( !(page.pageId in Config.structure) )
            {
                DebugWrapper.trace("WARNING: Switch to unavailable page: " + page.pageId);
                return;
            }

            var pageComponent:UIComponent = app.switchPage(page) as UIComponent;

            // Remove current mediator for clear it event listeners
            for each (var mediator:IMediator in mediators)
                facade.removeMediator(mediator.getMediatorName());

            // Register new page mediator
            mediators = [];
            var classes:Array = Config.structure[page.pageId].mediators;
            for each (var mediatorClass:Class in classes)
                registerMediator(mediatorClass, pageComponent);
        }

        private function registerMediator(mediatorClass:Class, viewComponent:UIComponent):void
        {
            if (mediatorClass)
            {
                var mediator:IMediator = new mediatorClass(viewComponent) as IMediator;
                var mediatorRegistred:Boolean = facade.hasMediator(mediator.getMediatorName());
                if (!mediatorRegistred)
                {
                    facade.registerMediator(mediator);
                    mediators.push(mediator);
                }
            }
        }


        //---------------------------------------------------------------------
        //  Event handler
        //---------------------------------------------------------------------

        private function app_switchPageHandler(event:PageEvent):void
        {
            switchPage(event.pageDescriptor);
        }
    }
}
